package it.scambiocorse.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Utils {

	
	public static boolean isNull (Object in) {
		return ((in == null)? true : false);
	}
	
	/**
	 * @method Restituisce true se diversa da null e da ""
	 *
	 */
	public static boolean isValid(String in) {
		return ((in != null && !in.equals("") )? true : false);
	}
	
	public static String getValid(String in) {
		String out = null;
		if (in == null) out = "";
		else out = in;
		return out;
	}
	
	public static ArrayList<LabelValueBean> getListaMinuti() {
		ArrayList<LabelValueBean> list = new ArrayList<LabelValueBean>();
//		list.add(new LabelValueBean("-",""));
		for (int i=0;i<=59;i+=5) {
			list.add(new LabelValueBean(""+((i<10) ?"0"+i : i),""+((i<10) ?"0"+i : i))); //il ternario aggiunge lo 0 (00, 01, 02...ecc)
		}
		
		return list;
	}
	
	public static ArrayList<LabelValueBean> getListaOra() {
		ArrayList<LabelValueBean> list = new ArrayList<LabelValueBean>();
//		list.add(new LabelValueBean("-",""));
		for (int i=0;i<=23;i++) {
			list.add(new LabelValueBean(""+((i<10) ?"0"+i : i),""+((i<10) ?"0"+i : i)));
		}
		
		return list;
	}
	
  public static Timestamp dataStringTimestamp(String data, String formato) {
  		Date TmpData = null;
	  	
	  	try {
	  			SimpleDateFormat sdf = new SimpleDateFormat(formato);
	  			TmpData = sdf.parse(data);
	  	} catch (Exception e) {
	  	}
	
	  return new Timestamp(TmpData.getTime());
  }	
  
  public static String dataTimestampString(Date t)
  {
	  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      return sdf.format(t);
  }
  
  
  
  public static Timestamp getSysDate()
  {
      return new Timestamp((new Date()).getTime());
  }
  
  public static String getSysDateStr()
  {
	  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      return sdf.format(new Date());
  }
  
  


  /**
   * Valida il contenuto di una data (nel formato dd/MM/yyyy) dal punto di vista formale (ad esempio blocca la data 30/04/2008)
   *
   * @param String - Data da validare
   *
   * @return boolean
   */
   public static boolean isValidDate(String data) 
   {
  	 	 if (Utils.isValid(data)) {
       boolean flag = true;
       if (data.length() > 10)
       {
           return false;
       }
       else
       {
           java.util.StringTokenizer strTok = new java.util.StringTokenizer(data, "/");
           int nrTok = strTok.countTokens();
           if (nrTok != 3)
           {
               return false;
           }
           String zi = strTok.nextToken();
           String luna = strTok.nextToken();
           String an = strTok.nextToken();
           try
           {
               if ((zi.length() != 2) || (luna.length() != 2) || (an.length() != 4))
               {
                   return false;
               }
           }
           catch (NumberFormatException e)
           {
               return false;
           }
           int maxDay = 31;
           int zk = Integer.parseInt(zi);
           int lk = Integer.parseInt(luna);
           int ak = Integer.parseInt(an);
           if (lk < 1 || lk > 12)
           {
               return false;
           }
           if ((luna.equals("04")) || (luna.equals("06")) || (luna.equals("09")) || (luna.equals("11")))
           {
               maxDay = 30;
           }
           else
           {
               if (luna.equals("02"))
               {
                   if ((ak % 4) > 0)
                   {
                       maxDay = 28;
                   }
                   else
                   {
                       if (((ak % 100) == 0) && ((ak % 400) > 0))
                       {
                           maxDay = 28;
                       }
                       else
                       {
                           maxDay = 29;
                       }
                   }
               }
           }
           if ((zk > maxDay) || (zk < 1))
           {
               flag = false;
           }
           return flag;
       }
  	 	} else return false;
   }
   
   
	public static String toMd5(String input){
	     String res = "";
	     try {
	         MessageDigest algorithm = MessageDigest.getInstance("MD5");
	         algorithm.reset();
	         algorithm.update(input.getBytes());
	         byte[] md5 = algorithm.digest();
	         String tmp = "";
	         for (int i = 0; i < md5.length; i++) {
	             tmp = (Integer.toHexString(0xFF & md5[i]));
	             if (tmp.length() == 1) {
	                 res += "0" + tmp;
	             } else {
	                 res += tmp;
	             }
	         }
	     } catch (NoSuchAlgorithmException ex) {}
	     return res;
	 }
	
	
	
	
	
	
	
		 
		
	 
		  public static boolean Time24HoursValidator(String time){
			  
			  boolean ret = false;
			  
			  try {
				  
			   Pattern pattern;
			   Matcher matcher;
			   String TIME24HOURS_PATTERN = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
			  pattern = Pattern.compile(TIME24HOURS_PATTERN);
			  matcher = pattern.matcher(time);
			  ret = matcher.matches();
			  
		  } catch (Exception e) {}


			  return ret;
	}	

//   public getCodiEsito
//		if (coda != null) {
//			logger.warn("Coda gia' esistente per la doppietta utente/servizio, controllo il livello della coda");
//			if (coda.getEsito().equals("IN ATTESA"))
//					tipoOpe = 2;
//			else if (coda.getEsito().equals("KO")) 
//				tipoOpe = 3;
//			else if (coda.getEsito().equals("OK")) 
//					tipoOpe = 4;
//			
//		}
}
